import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:winkai/entity/subscribe_from.dart';
import 'package:winkai/helper/iap/iap_helper.dart';
import 'package:winkai/res/app_colors.dart';
import 'package:winkai/res/dimens.dart';
import 'package:winkai/route/route_manifest.dart';
import 'package:winkai/ui/root/controller/root_controller.dart';
import 'package:winkai/utils/image_utils.dart';
import 'package:winkai/utils/page_jump_utils.dart';

class RootTitleWidget extends StatefulWidget {
  const RootTitleWidget({super.key});

  @override
  State<RootTitleWidget> createState() => _RootTitleWidgetState();
}

class _RootTitleWidgetState extends State<RootTitleWidget> {
  final controller = Get.find<RootController>();

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      left: false,
      right: false,
      bottom: false,
      child: Container(
        height: 48.w,
        padding: EdgeInsets.symmetric(horizontal: Dimens.mainHorizontalSpacing),
        child: Row(
          children: [
            GestureDetector(
              onTap: () {
                Get.toNamed(RouteManifest.settingPage);
              },
              child: ImageUtils.getImage('ic_ai_root_more', 32.w, 32.w),
            ),
            Expanded(
              child: Align(
                alignment: Alignment.center,
                child: TabBar(
                  controller: controller.tabController,
                  indicator: BoxDecoration(
                      borderRadius: BorderRadius.circular(3),
                      color: AppColors.mainColor),
                  isScrollable: true,
                  indicatorPadding: EdgeInsets.only(
                      left: 25.w, right: 25.w, top: 25.w, bottom: 15.w),
                  indicatorSize: TabBarIndicatorSize.tab,
                  labelPadding: EdgeInsets.symmetric(horizontal: 12.w),
                  labelStyle: TextStyle(
                    fontSize: 18.sp,
                    color: AppColors.black1a,
                    fontFamily: 'LexendDeca-Black',
                    fontWeight: FontWeight.w900,
                  ),
                  unselectedLabelStyle: TextStyle(
                    fontSize: 18.sp,
                    color: AppColors.black1a.withOpacity(0.6),
                    fontFamily: 'LexendDeca-Black',
                    fontWeight: FontWeight.w900,
                  ),
                  tabs: _buildTabBarItem(),
                ),
              ),
            ),
            Obx(() {
              IAPHelper().subscribeStatus.value;
              var isSubscribeVip = IAPHelper().isVip;
              return isSubscribeVip
                  ? SizedBox(
                      width: 36.w,
                      height: 36.w,
                    )
                  : GestureDetector(
                      onTap: () {
                        PageJumpUtils.toVipPage(SubscribeFrom.homeVip);
                      },
                      child: ImageUtils.getImage('ic_ai_root_vip', 36.w, 36.w),
                    );
            }),
          ],
        ),
      ),
    );
  }

  List<Widget> _buildTabBarItem() {
    List<String> titles = [
      'root_discover'.tr,
      'root_chat'.tr,
    ];
    var list = List.generate(titles.length, (index) {
      return Tab(
        text: titles[index],
      );
    });
    return list;
  }
}
